﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace WorldMap
{
    [Serializable]
    public class TileData
    {
        public string name;
        public List<string> Keys;
        public List<TileElementsData> elements;

        public ulong resTags = ulong.MinValue;

        public TileData(string name)
        {
            this.name = name;
            Keys = new List<string>();
            elements = new List<TileElementsData>();
        }

        public TileElementsData GetOrCreateElementListByName(string assetName)
        {
            var index = Keys.IndexOf(assetName);
            if (index == -1)
            {
                var tileElementData = new TileElementsData(assetName);
                this.addNewTile(assetName,tileElementData); 
                return tileElementData;
            }
            return elements[index];
        }

        public void addNewTile(string name, TileElementsData data)
        {
            if (Keys.Count == elements.Count)
            {
                Keys.Add(name);
                elements.Add(data);
            }
            else
            {
                Debug.LogError("存储出问题了，key和elements的数量对不上了");
            }
       
        }
    }
    [Serializable]
    public class TileElementsData
    {
        public int id;
        public string name;
        public List<TileElementsProperty> properties;

        public TileElementsData(string name)
        {
            this.name = name;
            properties = new List<TileElementsProperty>();
        }

        public void addElementProperty(TileElementsProperty property)
        {
            this.properties.Add(property);
        }
    
    }
    [Serializable]
    public class TileElementsProperty
    {
        public Vector3 position;
        public Vector3 rotation;
        public Vector3 scale;

        public TileElementsProperty(Vector3 p,Vector3 r,Vector3 s)
        {
            this.position = p;
            this.position.x = (float) Math.Round(position.x, 2, MidpointRounding.AwayFromZero);
            this.position.y = (float) Math.Round(position.y, 2, MidpointRounding.AwayFromZero);
            this.position.z = (float) Math.Round(position.z, 2, MidpointRounding.AwayFromZero);
            this.rotation = r;
            this.scale = s;
        }
    }
}
